﻿Public Class Memo

#Region "添加模式"

    Public Overrides Function DoAddExist() As Boolean
        MyBase.DoAddExist()
        DoAddExist = True
        Dim rs As ADODB.Recordset = Nothing
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine("SELECT TITLE FROM MEMO  WHERE TITLE=?")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput)
        If Me.txtSonger.Text.Trim().ToUpper().Length > 0 Then
            pa.Size = Me.txtSonger.Text.Trim().ToUpper().Length
        Else
            pa.Size = 10
        End If
        pa.Value = Me.txtSonger.Text.Trim().ToUpper()
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        rs = ct.ExcCommandAsRs(cmd)
        If rs Is Nothing Then Exit Function
        If rs.EOF = False Then
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            rs = Nothing
            DoAddExist = False
            ShowMessageBox("已经存在")
            Exit Function
        End If
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
    End Function

    Public Overrides Function DoAddCheck() As Boolean
        ' MyBase.DoCheck()
        DoAddCheck = False

        If Me.txtSonger.Text.Trim = String.Empty Then
            ShowMessageBox("内容不能为空")
            Me.txtSonger.Focus()
            Exit Function
        End If

        If Me.cbo类型.Text.Trim = String.Empty Then
            ShowMessageBox("类型不能为空")
            Me.cbo类型.Focus()
            Exit Function
        End If

        DoAddCheck = True
    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" MEMO ")
        sb.AppendLine(" (")
        sb.AppendLine(" TITLE ")
        sb.AppendLine(" ,type ")
        sb.AppendLine(" ,USERNAME ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,AddTime ")
        sb.AppendLine(" ,OptUser ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ) ")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 100
        pa.Value = CMainModule.CmnToStr1(Me.txtSonger.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adSmallInt, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Value = (Me.cbo类型.SelectedIndex + 1).ToString()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            Me.bAdd = True '表示成功插入数据能刷新界面
            ShowMessageBox("插入成功")
            ClearForm()
            DoInsert = True
        Else
            DoInsert = False
        End If

    End Function

#End Region

#Region "重置按钮"
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ClearForm()
    End Sub
#End Region

#Region "清除模式"
    Public Sub ClearForm()
        Me.cbo类型.Text = ""
        Me.txtSonger.Text = ""
        txtSonger.Focus()
    End Sub
#End Region

#Region "更新模式"
    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()

        Me.Text = "备注修改"
        Me.cbo类型.Enabled = False

        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * FROM MEMO  WHERE id='" + CStr(Me.参数列表("ID")) + "'")
        If rs Is Nothing Then Exit Sub
        If rs.EOF Then GoTo Function_End
        Me.txtSonger.Text = rs.Fields("TITLE").Value.ToString()
        Me.cbo类型.SelectedIndex = CInt(rs.Fields("type").Value) - 1
Function_End:
        rs.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        rs = Nothing
    End Sub

    Public Overrides Function DoUpdateCheck() As Boolean
        DoUpdateCheck = False
        If Me.txtSonger.Text.Trim = String.Empty Then
            ShowMessageBox("内容不能为空")
            Me.txtSonger.Focus()
            Exit Function
        End If
        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        Return True
    End Function

    Public Overrides Function DoUpdate() As Boolean
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" UPDATE ")
        sb.AppendLine(" MEMO ")
        sb.AppendLine(" SET   TITLE = ? ")
        sb.AppendLine(" ,     OPTIME = ? ")
        sb.AppendLine(" ,     OptUser = ? ")
        sb.AppendLine(" WHERE id = ? ")

        Dim pa As ADODB.Parameter = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 30
        pa.Value = CMainModule.CmnToStr1(Me.txtSonger.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)


        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 30
        pa.Value = CStr(Me.参数列表("ID"))
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            Me.bAdd = True '表示成功插入数据能刷新界面
            ShowMessageBox("修改成功")
            Me.Close()
            DoUpdate = True
        Else
            ShowMessageBox("修改失败")
            Me.Close()
            DoUpdate = False
        End If
    End Function
#End Region

End Class
